//
//  HZL_SceneViewController.m
//  B_Project
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_SceneViewController.h"
#import "HZL_NetworkingHandle.h"
#import "HZL_ZoneDetailTableViewCell.h"
#import "HZL_FindModel.h"
#import "HZL_StoreDetailViewController.h"
#import "JSDropDownMenu.h"


@interface HZL_SceneViewController ()< UITableViewDataSource, UITableViewDelegate,JSDropDownMenuDataSource,JSDropDownMenuDelegate>
{
    NSMutableArray *_data1;
    NSMutableDictionary *dataDic;
    NSMutableArray *arr1;
    NSMutableArray *_data2;
    NSMutableArray *data2_id;

    NSString *abc;
    
    
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
}
@property (nonatomic, retain) UIButton *left;
@property (nonatomic, retain) UIButton *right;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, strong) NSString *body;
@property (nonatomic, copy) NSString *cityId;
@property (nonatomic, retain) NSMutableDictionary *showDic;//用来判断分组展开与收缩
@property (nonatomic, copy) NSString *key;
@property (nonatomic, assign) NSInteger index;


@property (nonatomic, strong) UITableView *typeTableView;
@property (nonatomic, strong) UITableView *zoneTableView;
@property (nonatomic, strong) NSMutableArray *typeArr;

// 刷新
@property (nonatomic, assign) NSInteger MJI;
@property (nonatomic, assign) BOOL isRefresh;
@property (nonatomic, retain) MJRefreshAutoStateFooter *footer;

@property (nonatomic, strong) NSString *liveCircleId;
@end

@implementation HZL_SceneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.dataArr = [NSMutableArray array];
    
    if (self.Id) {
        [self getMenuData];
    }
    [self creatTableView];
    [self loadMBProgress];
//    [self getMenuData];

}

#pragma mark - 创建menu
- (void)creatMenu
{
    JSDropDownMenu *menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:45];
    menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
//    menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    menu.dataSource = self;
    menu.delegate = self;
    
    [self.view addSubview:menu];
}
#pragma mark - 获取menu数据
- (void)getMenuData
{
    _data2 = [NSMutableArray array];
    data2_id = [NSMutableArray array];
    _data1 = [NSMutableArray array];
    dataDic = [NSMutableDictionary dictionary];


    // 获取分类数据
    for (HZL_FindModel *typeId in self.typeModelArr) {
        [data2_id addObject:typeId.iD];
    }

    for (HZL_FindModel *typeModel in self.typeModelArr) {
        [_data2 addObject:typeModel.name];
    }
    
    // 获取地区数据
//    NSMutableArray *zone = [NSMutableArray array];
    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    if (city == nil) {
        city = @"xiamen";
    }
    
    NSString *body = [NSString stringWithFormat:@"cityId=%@&enableFlag=1", city];
    [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getDistrictList" body:body completion:^(id result) {
        
        NSMutableArray *tempArr = result[@"body"][@"list"];
        
//        [ab setObject:@"全城" forKey:@"title"];
        for (NSMutableDictionary *dic1 in tempArr) {

            NSMutableDictionary *ab = [NSMutableDictionary dictionary];
            abc = dic1[@"name"];
            [ab setObject:abc forKey:@"title"];
            
            
            if ([dic1[@"liveCircles"] count] == 0) {
                NSArray *arr = [NSArray array];
                [ab setObject:arr forKey:@"data"];
                [_data1 addObject:ab];
            }else {
                NSMutableArray *arr = [NSMutableArray array];
                for (NSMutableDictionary *dic2  in dic1[@"liveCircles"]) {
                    
                    [arr addObject:dic2[@"name"]];
                    
                    
                    [ab setObject:arr forKey:@"data"];
                    [dataDic setObject:dic2[@"id"] forKey:dic2[@"name"]];
                
                }
                [_data1 addObject:ab];
                
            }
        }
        [_data1 addObject:@{@"title":@"全城", @"data":@[@"全城"], @"id":@""}];
        [dataDic setObject:@"" forKey:@"全城"];
        NSLog(@"lalallal%@", dataDic);
        NSLog(@"11111111111111111111111%@", _data1);

        [self creatMenu];
    }];
    

}
// 分区
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 2;
}

// 表视图显示时，是否需要两个表显示
-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    if (column==1) {
        return YES;
    }
    return NO;
}
// 表视图显示时，左边表显示比例
-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    if (column==1) {
        return 0.5;
    }
    
    return 1;
}
// 返回当前菜单左边表选中行
-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    if (column==0) {
        
        return _currentData1Index;
        
    }
    if (column==1) {
        
        return _currentData2Index;
    }
    
    return 0;
}
// 创建menu
- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column==1) {
        if (leftOrRight==0) {
            
            return _data1.count;
        } else{
            
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] count];
        }
    } else if (column==0){
        
        return _data2.count;
        
    }
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
            
        case 1:
            if ([_data1[0] objectForKey:@"data"]) {
                return @"全城";
            }else {
            return [[_data1[0] objectForKey:@"data"] objectAtIndex:0]
                ;
            }
            break;
        case 0: return _data2[0];
            break;
        
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==1) {
        if (indexPath.leftOrRight==0) {
            NSDictionary *menuDic = [_data1 objectAtIndex:indexPath.row];
            return [menuDic objectForKey:@"title"];
        } else{
            NSInteger leftRow = indexPath.leftRow;
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] objectAtIndex:indexPath.row];
        }
    } else  {
        
        return _data2[indexPath.row];
        
    }
}

#pragma mark - menu点击方法
- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 1) {
        
        if(indexPath.leftOrRight==0){
            arr1 = [NSMutableArray array];
            _currentData1Index = indexPath.row;
            arr1 = _data1[indexPath.row][@"data"];
            
            return;
        }else if (indexPath.leftOrRight== 1){
            self.liveCircleId = [dataDic objectForKey:arr1[indexPath.row]];
            
            [self getData];
            [self.tableView.mj_header beginRefreshing];
        }
        
    } else {
        
        _currentData2Index = indexPath.row;
        
        self.Id = data2_id[indexPath.row];
        [self getData];
        [self.tableView.mj_header beginRefreshing];

    }
}


//- (void)creatView
//{
//    self.left = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.left.frame = CGRectMake(0, 64, VIEW_WIDTH / 2, 50);
//    [self.left setTitle:@"下午茶" forState:UIControlStateNormal];
//    [self.left setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.view addSubview:self.left];
//    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2, 74, 1, 30)];
//    label.backgroundColor = [UIColor grayColor];
//    [self.view addSubview:label];
//    
//    self.right = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.right.frame = CGRectMake(VIEW_WIDTH / 2, 64, VIEW_WIDTH / 2, 50);
//    [self.right setTitle:@"全城" forState:UIControlStateNormal];
//    [self.right setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.view addSubview:self.right];
//}

#pragma mark - 创建tableView
- (void)creatTableView
{
    if (self.Id) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 - 20, VIEW_WIDTH, VIEW_HEIGHT- 64 + 20) style:UITableViewStylePlain];
    }else {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
    }
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [UIView new];
//    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        self.MJI = 1;
        [self getData];
    }];
    [self.tableView.mj_header beginRefreshing];
    // 上拉加载
    self.footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        self.MJI ++;
        [self getData];
    }];
    self.tableView.mj_footer = self.footer;
}

- (void)getData
{
    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    if (city == nil) {
        city = @"xiamen";
    }
    NSString *str = [NSString stringWithFormat:@"currentPage=%ld", self.MJI];
    // 美食
    if (self.tastebar) {
        NSString *body = [NSString stringWithFormat:@"channelId=%@&cityId=%@&%@&device=iPhone7%%2C2&latitude=22.61667&longitude=114.06667&netWork=wifi&os=ios%%209.100000&pageSize=12",self.tastebar, city, str];
        [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getShopList" body:body completion:^(id result) {
            
            self.hud.hidden = YES;
            if (!result) {
                [self.view bringSubviewToFront:self.alertBtn];
                return;
            }
            // 下拉刷新
            if (self.isRefresh) {
                [self.dataArr removeAllObjects];
            }
            // 上拉加载
            NSMutableArray *tempArr = result[@"body"][@"list"];
            if ([tempArr count] != 0) {
                
                [self.dataArr addObjectsFromArray:[HZL_FindModel transformWithArray:tempArr]];
                [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
            }else {
                // 是否加载完
                [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
            }
            self.alertBtn.hidden = YES;
            [self.hud removeFromSuperview];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }];
    }
    // 休闲
    if (self.tongue) {
        NSString *body = [NSString stringWithFormat:@"channelId=%@&cityId=%@&%@&device=iPhone7%%2C2&latitude=22.61667&longitude=114.06667&netWork=wifi&os=ios%%209.100000&pageSize=12",self.tongue, city, str];
        [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getShopList" body:body completion:^(id result) {
            self.hud.hidden = YES;
            if (!result) {
                [self.view bringSubviewToFront:self.alertBtn];
                return;
            }
            // 下拉刷新
            if (self.isRefresh) {
                [self.dataArr removeAllObjects];
            }
            // 上拉加载
            NSMutableArray *tempArr = result[@"body"][@"list"];
            if ([tempArr count] != 0) {
                
                [self.dataArr addObjectsFromArray:[HZL_FindModel transformWithArray:tempArr]];
                [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
            }else {
                // 是否加载完
                [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
            }
            self.alertBtn.hidden = YES;
            [self.hud removeFromSuperview];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }];
    } // 全部
    if (self.allId) {
        NSString *body = [NSString stringWithFormat:@"categoryId=%@&cityId=%@&%@&device=iPhone7%%2C2&latitude=38.8835508765332&longitude=121.5443481132677&netWork=wifi&os=ios%%209.100000&pageSize=12",self.allId, city, str];
        [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getShopList" body:body completion:^(id result) {
            self.hud.hidden = YES;
            if (!result) {
                [self.view bringSubviewToFront:self.alertBtn];
                return;
            }
            // 下拉刷新
            if (self.isRefresh) {
                [self.dataArr removeAllObjects];
            }
            // 上拉加载
            NSMutableArray *tempArr = result[@"body"][@"list"];
            if ([tempArr count] != 0) {
                
                [self.dataArr addObjectsFromArray:[HZL_FindModel transformWithArray:tempArr]];
                [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
            }else {
                // 是否加载完
                [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
            }
            self.alertBtn.hidden = YES;
            [self.hud removeFromSuperview];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }];
    }else {
        
        if (self.liveCircleId == nil) {
            self.body = [NSString stringWithFormat:@"cityId=%@&%@&device=iPhone7%%2C2&latitude=22.61667&longitude=114.06667&netWork=wifi&os=ios%209.100000%&pageSize=12&sceneId=%@", city, str, self.Id];
        }else {
            self.body = [NSString stringWithFormat:@"cityId=%@&%@&device=iPhone7%%2C2&latitude=38.88627289937313&liveCircleId=%@&longitude=121.5443755206657&netWork=wifi&os=ios%%209.100000&pageSize=12&sceneId=%@", city, str, self.liveCircleId, self.Id];
        }
        [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getShopList" body:self.body completion:^(id result){
            self.hud.hidden = YES;
            if (!result) {
                [self.view bringSubviewToFront:self.alertBtn];
                return;
            }
            // 下拉刷新
            if (self.isRefresh) {
                [self.dataArr removeAllObjects];
            }
            // 上拉加载
            NSMutableArray *tempArr = result[@"body"][@"list"];
            if ([tempArr count] != 0) {
                
                [self.dataArr addObjectsFromArray:[HZL_FindModel transformWithArray:tempArr]];
                [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
            }else {
                // 是否加载完
                [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
            }
            self.alertBtn.hidden = YES;
            [self.hud removeFromSuperview];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }];
    }
    
}
- (void)changeCity:(NSNotification *)note
{
    self.cityId = note.object;
    [self.tableView.mj_header beginRefreshing];
    [self getData];
}
#pragma mark - tableView方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HZL_ZoneDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[HZL_ZoneDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (VIEW_HEIGHT - 64 - 50 -10) / 2.8 + 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZL_StoreDetailViewController *sd = [[HZL_StoreDetailViewController alloc] init];
    sd.iD= [self.dataArr[indexPath.row] iD];
    sd.findModel =self.dataArr[indexPath.row];
    [self.navigationController pushViewController:sd animated:YES];
}
 

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
